﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSemIII.Administrator.Survey
{
    public partial class ManageSurvey : System.Web.UI.Page
    {
        private SurveyProcess surveyProcess;
        private List<Entity.Survey> listSurvey;
        private const bool blnAllowPaging = true;
        private const int iPageSize = 5;
        private static PagedDataSource pgdProducts;
        protected void Page_Load(object sender, EventArgs e)
        {
            surveyProcess = new SurveyProcess();
            pgdProducts = new PagedDataSource();
            this.updateStatusSurveys();
            if (!Page.IsPostBack)
            {
                this.getAllSurvey();
            }
        }

        private void updateStatusSurveys()
        {
            listSurvey = surveyProcess.getAllSurvey();
            foreach (Entity.Survey s in listSurvey)
            {
                if (s.SurveyStatus == 3)
                {
                    if (DateTime.Compare(s.SurveyStartDate, DateTime.Now) == 0 || DateTime.Compare(s.SurveyStartDate, DateTime.Now) == -1)
                    {
                        int result = surveyProcess.updateStatusSurvey(s.SurveyID, 1);
                        if (result == 10)
                        {

                        }
                        else
                        {

                        }
                    }
                    else if (DateTime.Compare(s.SurveyStartDate, DateTime.Now) == 1)
                    {
                        int result = surveyProcess.updateStatusSurvey(s.SurveyID, 2);
                        if (result == 10)
                        {

                        }
                        else
                        {

                        }
                    }
                }
            }
        }

        private void getAllSurvey()
        {
            listSurvey = surveyProcess.getAllSurvey();

            pgdProducts.AllowPaging = blnAllowPaging;
            pgdProducts.PageSize = iPageSize;
            pgdProducts.DataSource = listSurvey;

            pgdProducts.CurrentPageIndex = ThisPageNumber;
            fillTotalPage(pgdProducts.PageCount);

            allSurvey.DataSource = pgdProducts;
            allSurvey.DataBind();
        }

        private void fillTotalPage(int iTotalPages)
        {
            List<int> pageID = new List<int>();
            for (int i = 1; i <= iTotalPages; i++)
            {
                pageID.Add(i);
            }
            totalPage.DataSource = pageID;
            totalPage.DataBind();
        }

        private int ThisPageNumber
        {
            get
            {
                object obj = ViewState["ThisPageNumber"];
                return (obj == null) ? 0 : (int)obj;
            }
            set
            {
                this.ViewState["ThisPageNumber"] = value;
            }
        }

        protected void allSurvey_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int idSurvey = Convert.ToInt32(e.CommandArgument);
            Label lbStatus = (Label)e.Item.FindControl("lbStatus");
            if (e.CommandName == "Delete")
            {
                if (lbStatus.Text == "Opening")
                {
                    lbError.ForeColor = System.Drawing.Color.Red;
                    lbError.Text = "Can't delete survey if status is opening!";
                }
                else
                {
                    int result = surveyProcess.deleteSurveyBySurveyID(idSurvey);
                    if (result == 10)
                    {
                        lbError.ForeColor = System.Drawing.Color.Black;
                        lbError.Text = "Delete survey success!";
                        this.getAllSurvey();
                    }
                    else
                    {
                        lbError.ForeColor = System.Drawing.Color.Red;
                        lbError.Text = "An error in delete process!";
                    }
                }
            }
            else if (e.CommandName == "Edit")
            {
                if (lbStatus.Text == "Closed")
                {
                    lbError.ForeColor = System.Drawing.Color.Red;
                    lbError.Text = "Can't edit survey if status is closed!";
                }
                else
                {
                    int result = surveyProcess.updateStatusSurvey(idSurvey, 3);
                    if (result == 10)
                    {
                        Response.Redirect("EditSurvey.aspx?surveyid=" + idSurvey);
                    }
                    else
                    {
                        lbError.Text = "Error in update status survey process!";
                    }
                }
                //ClientScript.RegisterStartupScript(GetType(), "openEditSurveyPage", "window.open('EditSurvey.aspx?surveyid=" + idSurvey + "');", true);
            }
            else
            {
                if (lbStatus.Text == "Opening" || lbStatus.Text == "Wait Open")
                {
                    lbError.ForeColor = System.Drawing.Color.Red;
                    lbError.Text = "Can't view survey if status is opening or Wait Open!";
                }
                else
                {
                    Response.Redirect("ViewResult.aspx?surveyid=" + idSurvey + "&count=0");
                }
            }
        }

        protected void btPrev_Click(object sender, EventArgs e)
        {
            LinkButton lbuton = (LinkButton)sender;
            if (lbuton.ID == "btPrev")
            {
                if (ThisPageNumber > 0)
                {
                    ThisPageNumber = ThisPageNumber - 1;
                    this.getAllSurvey();
                }
            }
            //Change page when user click to next number button
            else if (lbuton.ID == "btNext")
            {
                if (ThisPageNumber < totalPage.Items.Count - 1)
                {
                    ThisPageNumber = ThisPageNumber + 1;
                    this.getAllSurvey();
                }
            }
        }

        protected void PageNumber_Click(object sender, EventArgs e)
        {
            LinkButton lbuton = (LinkButton)sender;
            ThisPageNumber = Convert.ToInt32(lbuton.Text) - 1;
            this.getAllSurvey();
        }
    }
}